<?php

namespace app\group\model\goods;

use sheep\model\Common;
use think\model\concern\SoftDelete;

class Goods extends Common
{
    use SoftDelete;

    protected $name = 'group_goods';

    protected $type = [
        'category_id'  => 'integer',
        'validity'     => 'integer',
        'subscribe'    => 'integer',
        'product_list' => 'json',
    ];

//    protected $append = ['category_id_text'];

    public function getStatusAttr($value)
    {
        $status = [
            'up'   => '上架',
            'down' => '下架',
        ];
        return ['text' => $status[$value], 'value' => $value];
    }

    public function getIsLimitAttr($value)
    {
        $is_limit = [
            0 => '不限购',
            1 => '限购',
        ];
        return ['text' => $is_limit[$value], 'value' => $value];
    }

    public function getIsDiscountAttr($value)
    {
        $is_discount = [
            0 => '无折扣',
            1 => '有折扣',
        ];
        return ['text' => $is_discount[$value], 'value' => $value];
    }

//    public function getCategoryIdAttr($value, $data)
//    {
//        $categories = $this->categoryList();
//        foreach ($categories as $category) {
//            foreach ($category['children'] as $child) {
//                if ($child['id'] === $value) {
//                    return $child;
//                }
//            }
//        }
//    }

//    public function getCategoryIdTextAttr($value, $data)
//    {
//        $categories = $this->categoryList();
//        foreach ($categories as $category) {
//            foreach ($category['children'] as $child) {
//                if ($child['id'] === $data['category_id']) {
//                    return $child['title'];
//                }
//            }
//        }
//    }


    /**
     * 团购列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList($param = [])
    {
        $model = $this;
        $where = [];
        if (!empty($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        // 服务分类
        if (!empty($param['categoryId'])) {
            $where[] = ['category_id', '=', $param['categoryId']];
        }
        // 服务名称
        if (!empty($param['keyword'])) {
            $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
        }
        // 适用宠物
        if (!empty($param['apply'])) {
            $where[] = ['apply', 'like', '%' . $param['apply'] . '%'];
        }
        // 状态
        if (!empty($param['status'])) {
            $where[] = ['status', '=', $param['status']];
        }

        // 创建人
        if (!empty($param['create_people'])) {
            $where[] = ['create_people', 'like', '%' . $param['create_people'] . '%'];
        }

        // 创建时间
//        if (!empty($param['start_time']) || !empty($param['end_time']))
//        {
//            $where[] = ['create_time','between', [strtotime($param['start_time']), strtotime($param['end_time'])]];
//        }
        if (!empty($param['start_time'])) {
            $where[] = ['create_time', '>', strtotime($param['start_time'])];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['create_time', '<=', strtotime($param['end_time'])];
        }

        return $model->order('id desc')->where($where)->paginate($param);
    }

    /**
     * 服务分类
     * @return array
     */
    public function categoryList()
    {
        return [
            [
                'id'       => 1501,
                'title'    => '宠物医院',
                'children' => [
                    [
                        'id'    => 1503,
                        'title' => '绝育',
                    ],
                    [
                        'id'    => 1504,
                        'title' => '疫苗',
                    ],
                    [
                        'id'    => 1505,
                        'title' => '体检',
                    ],
                    [
                        'id'    => 1506,
                        'title' => '驱虫',
                    ],
                    [
                        'id'    => 1507,
                        'title' => '诊疗',
                    ],
                    [
                        'id'    => 1508,
                        'title' => '洁牙',
                    ],

                ]
            ],
            [
                'id'       => 1502,
                'title'    => '宠物美容',
                'children' => [
                    [
                        'id'    => 1509,
                        'title' => '洗澡',
                    ],
                    [
                        'id'    => 1510,
                        'title' => '美容',
                    ],
                    [
                        'id'    => 1511,
                        'title' => '洁牙',
                    ],
                    [
                        'id'    => 1512,
                        'title' => '点项护理',
                    ],
                ]
            ],
        ];
    }

    public static function hairList()
    {
        return [
            [
                'id'    => 1,
                'title' => '短毛',
            ],
            [
                'id'    => 2,
                'title' => '长毛',
            ],
            [
                'id'    => 3,
                'title' => '不限制',
            ],
        ];
    }

    /**
     * 添加
     * @param $data
     * @return Goods|\think\Model
     */
    public function dataCreate($data)
    {
        return $this->create($data);
    }

    /**
     * 详情
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function detail($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * 修改
     * @param $data
     * @return Goods|\think\Model
     */
    public function dataEdit($data)
    {
        return self::update($data);
    }

    /**
     * 状态修改
     * @param $data
     * @return Goods|\think\Model
     */
    public function dataStatusEdit($data)
    {
        return self::update($data);
    }

    /**
     * 入库
     * @param $data
     * @return Goods|\think\Model
     */
    public function inStock($data)
    {
        return self::where('id', $data['id'])->inc('stocks', $data['stocks'])->update();
    }

    /**
     * 出库
     * @param $data
     * @return Goods|\think\Model
     */
    public function outStock($data)
    {
        return self::where('id', $data['id'])->dec('stocks', $data['stocks'])->update();
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function deleteData($id)
    {
        return self::destroy($id);
    }


}